package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 学员的条件分页查询
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log.info("学员的条件分页查询:{}",studentQueryParam);
        PageResult<Student> pageResult =  studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 添加学员
     */
    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("添加学员:{}",student);
        studentService.save(student);
        return Result.success();
    }

    /**
     * 根据id查询学员
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询学员:{}",id);
        Student student =  studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学员信息
     */
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改学员信息:{}",student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 批量删除学员
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("批量删除学员:{}",ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 学员违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violationHandle(@PathVariable Integer id,@PathVariable Integer score){
        log.info("学员违纪处理:{},{}",id,score);
        studentService.violationHandle(id,score);
        return Result.success();
    }
}
